<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller
{
	function Home()
	{
		parent::__construct();
        $this->load->model('common/common_model');
		$this->load->model('home/home_model');
        
		$this->load->library('Mydatetime');
        $this->load->library('Myinfo');
	}
	
	function index()
	{
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }
        $data = array();
        $userid = $this->session->userdata('uid');
        $user_oasis_orderid = $this->session->userdata('oasis_orderid');
        $c = (int)$this->input->get('c');
        if($c!=$user_oasis_orderid)
        {
            $oasisId = $this->common_model->ww_get_map_user_info($userid, $c);
            if($oasisId)
            {
                $this->session->set_userdata('oasis_orderid', $oasisId['oasis_orderid']);
            }
            else
            {
                redirect(site_url('home'));
                die();
            }
            $user_oasis_orderid = $oasisId['oasis_orderid'];
        }
        $data = $this->myinfo->loadUserinfo($userid, $user_oasis_orderid);
        /*
		*	check time 
		*
		*/
		$arrComplete = $arrNotComplete = $downTime = array();
		$train = $this->common_model->ww_get_train_user_info($userid, $user_oasis_orderid);
		foreach($train as $pstime)
		{
			$timestamp = $pstime['timestamp'] + $pstime['eachtime'];
			$timenow = time();
			if($timenow > $timestamp)
			{
				/* 
				* neu thoi gian hien tai lon hon thoi gian ket thuc nhiem vu => da ket thuc nhiem vu
				* insert len database da hoan thanh nhiem vu => lay array id cua nv de insert complete
				* kich hoat them don vi cho nguoi choi
				*/
				//->$this->home_model->ww_update_quantity_military($userid, $pstime['id']);
				$arrComplete[] = $pstime['id'];
			}
			else
			{
				/*
				* neu nhiem vu chua ket thuc
				* lay nhung id chua hoan thanh va show list
				* thoi gian chay = thoi gian tong(timestamp + eachtime) - thoi gian hien tai
				* cong tong cac thoi gian chay lai
				*/
				$pstime['tComplete'] = $pstime['timestamp'] + $pstime['eachtime'];
				$pstime['nice_timeComplete'] =  $this->mydatetime->nicetime(gmdate('Y-m-d H:i:s', $pstime['tComplete']));
				$downTime = !isset($arrNotComplete[0]) ? $pstime : null;
				$pstime['eachtime'] = ($pstime['timestamp'] + $pstime['eachtime']) - time();
				$arrNotComplete[] = $pstime;
				
			}
		}
		$data['info'] = $arrNotComplete;
		if(!empty($downTime))
		{
			if($downTime['eachtime'] == $downTime['lquantity']*$downTime['fixtime'])
			{
				$data['retime'] = $downTime['timestamp'] + $downTime['quantity']*$downTime['fixtime'] + $downTime['fixtime'] - $timenow;
			}
			else if($downTime['eachtime'] > $downTime['lquantity']*$downTime['fixtime'])
			{
				$data['retime'] = $downTime['timestamp'] + $downTime['eachtime'] - $downTime['lquantity']*$downTime['fixtime'] + $downTime['quantity']*$downTime['fixtime'] + $downTime['fixtime'] - $timenow;
			}
			else
			{
				$data['retime'] = $downTime['fixtime'];
			}
		}
		/*
		* get time chay don vi dau tien
		*/
		$data['chk_rtime'] = !empty($downTime) ? true : false;
		/*
		* lay thong tin so luong cac don vi hien tai dang co o vi tri nay
		*------------------------------------------------------------------------------
		*/
		
		//---------------
		$data['unit'] = $this->home_model->ww_get_units_user_info($userid, $user_oasis_orderid);
		
        
        /*
        * Show all army in oasis
        */
        
        $all_troop_in_oasis = $this->home_model->ww_get_all_troop_in_oasis($userid, $user_oasis_orderid, 0);
		$data['tt_troop'] = $all_troop_in_oasis;
        
        
        $lst_troop_in_oasis = $this->home_model->ww_get_all_troop_in_oasis($userid, $user_oasis_orderid, 1);
		$data['lst_troop'] = $lst_troop_in_oasis;
        
        /**/
        
		// header
		
		// content
		$this->load->view('home/home_view', $data);
		
		// footer
	}
	
	function add_military()
	{
	   if(!$this->session->userdata('username'))
       {
            redirect(site_url('login'));
            die();
       }
		// check session user
		$arr_unit = array(
			'u2', // linh bo binh
			'u3', // linh ban tia
			'u4', // linh chong tang
			'u5', // xe chien dau bo binh
			'u6', // xe thiet giap cho quan
			'u7', // xe thiet giap trinh sat
			'u8', // xe thiet giap chong min
			'u9', // xe thiet giap boc thep
			'u10', // xe tang hanh nhe
			'u11', // xe tang chien dau chu luc
			'u12', // sung coi
			'u13', // son phao
			'u14', // luu phao
			'u15', // phao phan luc
			'u16', // phao tu hanh
			'u17', // phao cao xa
			'u18', // phao phong khong tu hanh
			'u19', //
			'u20', //
			'u21', //
			'u22', //
			'u23', //
			'u24', //
			'u25', //
			'u26', //
			'u27', //
			'u28', //
			'u29', //
			'u30', //
			'u31', //
			'u32', //
			'u33', //
			'u34', //
			'u35', //
			'u36', //
			'u37', //
			'u38', //
			'u39', //
			'u40', //
			'u41', //
			'u42', //
			'u43', //
			'u44', //
			'u45', //
			'u46', //
			'u47', //
			'u48', //
			'u49', //
			'u50', //
			'u51', //
			'u52', //
			'u53', //
			'u54', //
			'u55', //
			'u56', //
			'u57', //
			'u58', //
			'u59', //
			'u60', //
			'u61', //
			'u62', //
			'u63', //
			'u64', //
			'u65', //
		);
		
		$unit = $this->input->post('obj');
		$val = (int)$this->input->post('val');
        $coor = (int)$this->input->post('coor');
		
		// check ton tai trong unit
		$chk_arrUnit = !in_array($unit, $arr_unit) ? true : false;
		// check tai nguyen co du de mua khong
		
		// check don vi da dc unlock chua
		
		// tru tai nguyen
		
		// update them don vi
		
		//check lai
		$userid = $this->session->userdata('uid');
        $oasisId = $this->home_model->ww_get_map_user_info($userid, $coor);
        if(!$oasisId)
        {
            echo 'error';
            die();
        }
		$unit = substr($unit,1);
		$quantity = $lquantity = $val;
		$timestamp = time();
		$fixtime = 20;
		$status = 0;
		//
		$eachtime = $fixtime*$val;
		//trainning units
		$this->home_model->ww_update_training_military($userid, $coor, $unit, $lquantity, $timestamp, $eachtime, $fixtime, $status);
		
		//update units
		//$this->home_model->ww_update_quantity_military($userid, $land, $pos, $unit, $val);
	}
	
	function training()
	{
		// neu session ko co thi thoat
		/*
		*	$uid -> id nguoi choi;
		*	$troop_code -> code cua binh linh
		*	$quantity -> so luong
		*	$timestamps -> thoi gian mua
		*	$eachtime-> thoi gian ra qua
		*/
		$info = $this->home_model->add_training_military($uid, $troop_code, $quantity, $timestamps, $eachtime);
	}
}